package com.iqiao.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iqiao.common.exception.ParamsException;
import com.iqiao.common.exception.ResetException;
import com.iqiao.common.web.Constant;
import com.iqiao.common.web.result.ReturnCode;
import com.iqiao.platform.dao.MenuDao;
import com.iqiao.platform.model.Admin;
import com.iqiao.platform.model.Menu;
import com.iqiao.platform.service.AdminService;
import com.iqiao.platform.service.MenuService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private AdminService adminService;

    @Override
    @Transactional(readOnly = true)
    public List<Menu> findAll(Map<String, Object> params) {
        return menuDao.findAll(params);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Menu> findPage(Map<String, Object> params) {
        int pageNo = MapUtils.getIntValue(params, "pageNo", Constant.PAGE_NO);
        int pageSize = MapUtils.getIntValue(params, "pageSize", Constant.PAGE_SIZE);
        return this.findPage(pageNo, pageSize, params);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Menu> findPage(int pageNo, int pageSize, Map<String, Object> params) {
        params.put("typeIn", StringUtils.split(MapUtils.getString(params, "typeIn"), ","));
        PageHelper.startPage(pageNo, pageSize);
        List<Menu> menuList = menuDao.findAll(params);
        return new PageInfo<>(menuList);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Menu> findByRoleId(Long roleId) {
        return menuDao.findByRoleId(roleId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Menu> findByUserId(Long userId) {
        Admin admin = adminService.findByUserId(userId);
        if (null == admin) {
            return new ArrayList<>();
        }
        return menuDao.findByRoleId(admin.getRoleId());
    }

    @Override
    @Transactional
    public Menu save(Menu menu) {
        // 检验parentId是否存在
        if (menu.getParentId() != null && menu.getParentId() != 0) {
            boolean isParentExist = menuDao.exist(menu.getParentId());
            if (!isParentExist) {
                throw new ParamsException(ReturnCode.MENU_PARENT_NO_EXIST, "上级菜单不存在！");
            }
        } else {
            menu.setParentId(0L);
        }
        // 检验code是否已经存在
        boolean isCodeExist = menuDao.existByCode(menu.getCode());
        if (isCodeExist) {
            throw new ParamsException(ReturnCode.MENU_CODE_ALERT_EXIST, "菜单编号已存在！");
        }
        menuDao.save(menu);
        return menu;
    }

    @Override
    @Transactional
    public Menu modify(Menu menu) {
        // 检验修改的菜单是否存在
        Menu oldMenu = menuDao.findById(menu.getId());
        if (null == oldMenu) {
            throw new ParamsException(ReturnCode.MENU_NO_EXIST, "菜单不存在");
        }
        // 检验parentId是否存在
        if (menu.getParentId() != null && menu.getParentId() != 0) {
            boolean isParentExist = menuDao.exist(menu.getParentId());
            if (!isParentExist) {
                throw new ParamsException(ReturnCode.MENU_PARENT_NO_EXIST, "上级菜单不存在！");
            }
        } else {
            menu.setParentId(0L);
        }
        // 检验code是否已经存在
        if (!Objects.equals(menu.getCode(), oldMenu.getCode())) {
            boolean isCodeExist = menuDao.existByCode(menu.getCode());
            if (isCodeExist) {
                throw new ParamsException(ReturnCode.MENU_CODE_ALERT_EXIST, "菜单编号已存在！");
            }
        }
        menuDao.modify(menu);
        return menu;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        menuDao.delete(id);
    }
}
